package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.TrainingDateAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.TrainingDetailPagerAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.TrainingRouteAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.TrainingListDetailBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;
import com.shixi.shixitraining.shixitraining.view.recycleview.BaseAdapter;

import java.util.List;

import retrofit2.Call;

public class TrainingDetailActivity extends BaseActivity implements View.OnClickListener {

    private TabLayout mTab;
    private ViewPager mViewPager;
    private TextView mCollectTv;
    private ImageView mCollectIv;
    private TextView mApply;
    private RecyclerView mRcivewRoute;
    //    private LinearLayout mMoreData;
    private int firstItem = 1;
    public String destine;
    public String imageUrl;
    public String expense;
    private String title;
    private String outset_city;
    private List<TrainingListDetailBean.DataBean.DetailsBean> detailBean;
    private LinearLayout mCollectLin;
    private int routeId;
    private int dateId;
    private List<TrainingListDetailBean.DataBean.DetailsBean> detailsData;
    private TextView mPrice;
    private TrainingDetailPagerAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_detail);
        initView();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String trainingId = bundle.getString("id");
            initData(trainingId);
        }
    }

    private void initData(String trainingId) {
        HttpUtil.getInstance().getApiInterface().trainingDetail("http://api.shixigroup.com/training/view/id/" + trainingId).enqueue(new CallbackImple<TrainingListDetailBean>() {
            @Override
            public void onError(Call<TrainingListDetailBean> call, Throwable t) {
                ToastUtil.showShortToast(TrainingDetailActivity.this, t.getMessage());
            }

            @Override
            public void onSuccess(Call<TrainingListDetailBean> call, TrainingListDetailBean response) {
                if (response != null) {
                    if (response.getData() != null) {
                        imageUrl = response.getData().getImage();
                        destine = response.getData().getDestine();
                        detailBean = response.getData().getDetails();
                        expense = response.getData().getExpense();
                        String features = response.getData().getFeatures();
                        outset_city = response.getData().getOutset_city();
                        title = response.getData().getTitle();
                        adapter = new TrainingDetailPagerAdapter(getSupportFragmentManager(),response);
                        mTab.setTabMode(TabLayout.MODE_FIXED);
                        mTab.setupWithViewPager(mViewPager);
                        mViewPager.setAdapter(adapter);


                        final List<TrainingListDetailBean.DataBean.TrainingBean> trainingData = response.getData().getTraining();
                        List<TrainingListDetailBean.DataBean.ScheduleBean> scheduleBean = response.getData().getSchedule();
                        ImageView training_img = (ImageView) findViewById(R.id.training_detail_img);
                        Glide.with(TrainingDetailActivity.this).load(imageUrl).placeholder(R.drawable.banner_default).into(training_img);
                        TextView mTitle = (TextView) findViewById(R.id.training_title);
                        TextView mCity = (TextView) findViewById(R.id.training_detail_city);
                        TextView pice = (TextView) findViewById(R.id.training_pice);
                        mTitle.setText(title);
                        mCity.setText(outset_city);
//实训
                        mRcivewRoute = (RecyclerView) findViewById(R.id.rcview_rotue);
                        GridLayoutManager manager = new GridLayoutManager(TrainingDetailActivity.this, 1);
                        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                        mRcivewRoute.setLayoutManager(manager);
                        TrainingRouteAdapter adapter = new TrainingRouteAdapter(TrainingDetailActivity.this, trainingData);
                        mRcivewRoute.setAdapter(adapter);
                        adapter.setOnCustomItemClickListener(new BaseAdapter.OnCustomItemClickListener<TrainingListDetailBean.DataBean.TrainingBean>() {
                            @Override
                            public void onCustomItemClick(TrainingListDetailBean.DataBean.TrainingBean item, int position) {
                                routeId = item.getId();
                            }
                        });

                        detailsData = response.getData().getDetails();

                        //行程
                        try {
                            for (int i = 0; i < detailsData.size(); i++) {
                                TrainingListDetailBean.DataBean.DetailsBean detailsBean = detailsData.get(i);

                                String price = detailsBean.getPrice();
                                String date = detailsBean.getDate();
                                if (detailsData.equals("暂无团可报0")) {
                                    mPrice.setText("￥" + 0 + "起/人");
                                    mApply.setBackgroundColor(getResources().getColor(R.color.grayc5));
                                    mApply.setTextColor(getResources().getColor(R.color.grayc5));
                                } else {
                                    mPrice.setText("￥" + price + "起/人");
                                }

                                RecyclerView mRcviewDate = (RecyclerView) findViewById(R.id.rcview_date);
                                GridLayoutManager managerDate = new GridLayoutManager(TrainingDetailActivity.this, 1);
                                managerDate.setOrientation(LinearLayoutManager.HORIZONTAL);
                                mRcviewDate.setLayoutManager(managerDate);
                                TrainingDateAdapter adapterDate = new TrainingDateAdapter(TrainingDetailActivity.this, detailBean);
                                mRcviewDate.setAdapter(adapterDate);
                                adapterDate.setOnCustomItemClickListener(new BaseAdapter.OnCustomItemClickListener<TrainingListDetailBean.DataBean.DetailsBean>() {
                                    @Override
                                    public void onCustomItemClick(TrainingListDetailBean.DataBean.DetailsBean item, int position) {
                                        dateId = item.getId();
                                    }
                                });

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        ToastUtil.showShortToast(TrainingDetailActivity.this, response.getMsg());
                    }
                } else {
                    ToastUtil.showShortToast(TrainingDetailActivity.this, "数据为空");
                }
            }
        });

    }

    private void initView() {
        ImageView mBackImg = (ImageView) findViewById(R.id.iv_detail_back);
        mApply = (TextView) findViewById(R.id.tv_apply_training);
        mTab = (TabLayout) findViewById(R.id.tablayout);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mCollectLin = (LinearLayout) findViewById(R.id.lin_collection_training);
        mPrice = (TextView) findViewById(R.id.training_pice);
        mCollectTv = (TextView) findViewById(R.id.tv_collection_training);
        mCollectIv = (ImageView) findViewById(R.id.iv_collection_training);

//        mMoreData = (LinearLayout) findViewById(R.id.ll_more_data);
        mApply.setOnClickListener(this);
        mBackImg.setOnClickListener(this);
        mCollectLin.setOnClickListener(this);
//        mMoreData.setOnClickListener(this);
        initTab();
    }


    //初始化tab
    private void initTab() {

//        mTab.setTabMode(TabLayout.MODE_FIXED);
//        mTab.setupWithViewPager(mViewPager);
//        mViewPager.setAdapter(adapter);

    }


    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_apply_training:

                if (UserManager.getInstance().isUserLogged()) {
                    if (detailsData.equals("暂无无团可报")) {
//                        mApply.isEnabled()=false;
                        mApply.setBackgroundColor(getResources().getColor(R.color.grayc5));
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putInt("training", routeId);
                        bundle.putInt("details", dateId);
                        Intent intent = new Intent(this, PracticalApplicationActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        mApply.setText("已申请");
                        mApply.setBackgroundColor(getResources().getColor(R.color.grayc5));
                    }

                } else {
                    startActivity(new Intent(this, PracticalApplicationActivity.class));
                }
                break;
            case R.id.lin_collection_training:

                mCollectTv.setText("已收藏");
                mCollectTv.setTextColor(getResources().getColor(R.color.basepresscolor));
                Glide.with(this).load(R.drawable.icon_collect_selected).into(mCollectIv);
                break;
//            case R.id.ll_more_data:
//                 startActivity(new Intent(this,CalendarActivity.class));
//                break;
            case R.id.iv_detail_back:
                this.finish();
                break;

        }
    }


}
